package com.exam.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.FilterAnswer;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.exam.entity.Exam;
import com.exam.entity.dto.ExamDTO;
import com.exam.service.IExamService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mark.entity.StuAnswer;
import com.paper.entity.Paper;
import com.user.entity.dto.StudentDTO;
import com.user.entity.dto.TeacherDTO;
import com.user.service.ITeacherService;

@Controller
@RequestMapping("/exam")
public class ExamController {
	@Autowired
	private IExamService examService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<Exam> findPage(HttpSession session,JpaPageable pageable,String str,Long course_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(dto!=null)
				ids = teacherService.getCoursesId(dto.getId());
			return examService.findPage(ExamDTO.getSpecification(ids,str,course_id), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/save")
	public @ResponseBody AjaxResponse save(String exam) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			Exam obj = objectMapper.readValue(exam,Exam.class);
			Date now = new Date();
			if(now.getTime()>obj.getDate().getTime())
				return new AjaxResponse(false, "考试时间不符合规范!");
			if(examService.isNumExist(obj))
				return new AjaxResponse(false, "编号已存在!");
			if(obj.getId()!=null&&examService.isRunningOrEnd(obj.getId()))
				return new AjaxResponse(false, "考试已开始或结束，不允许修改!");
			examService.save(obj);
			return new AjaxResponse(true, "发布成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Exam findOne(Long id) {
		try {
			return examService.findOne(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(examService.isRunningOrEnd(id))
				return new AjaxResponse(false, "考试已开始或结束，不允许删除!");
			examService.delete(id);
			return new AjaxResponse(true, "已删除！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "删除失败...");
		}
	}
	@RequestMapping("/loadPaper")
	public @ResponseBody Paper loadPaper(Long exam_id) {
		try {
			return FilterAnswer.filterPaper(examService.loadPaper(exam_id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/loadStuAnswer")
	public @ResponseBody StuAnswer loadStuAnswer(HttpSession session,Long exam_id) {
		try {
			StudentDTO dto = (StudentDTO) session.getAttribute("student");
			if(exam_id!=null&&null!=dto&&dto.getId()!=null) {
				return FilterAnswer.filterStuAnswer(examService.loadStuAnswer(dto.getId(), exam_id));
			}else {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/getCurrentDate")
	public @ResponseBody Long getCurrentDate() {
		try {
			return examService.getCurrentDate().getTime();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
}
